package com.hxkj.dao.impl;

import com.hxkj.dao.UserDao;
import com.hxkj.domain.po.AuthFunction;
import com.hxkj.domain.po.AuthRole;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.Site;
import com.hxkj.mapper.AuthUserMapper;
import com.hxkj.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户持久层实现
 * <p>
 * Created by chenwei on 2018/5/15.
 */
@Service
public class UserDaoImpl implements UserDao {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthUserMapper authUserMapper;


    @Override
    public AuthUser findByAccount(String account) {
        return userMapper.findByAccount(account);
    }

    @Override
    public Set<String> findRolesByAccount(String account) throws Exception {
        return userMapper.findRolesByAccount(account);
    }

    @Override
    public Set<String> findPermissionsByAccount(String account) throws Exception {
        return userMapper.findPermissionsByAccount(account);
    }

    @Override
    public AuthRole getRoleByAccount(AuthUser authUser) {
        return userMapper.getRoleByAccount(authUser);
    }

    @Override
    public List<AuthFunction> findFunctions(String account, String appKey,String siteId) {
        Map<String, String> map = new HashMap<>();
        map.put("account", account);
        map.put("appKey", appKey);
        map.put("siteId", siteId);
        return userMapper.findFunctions(map);
    }

    @Override
    public List<AuthRole> getRolesByAccount(String appCode, String account) {
        Map<String, String> map = new HashMap<>();
        map.put("appCode", appCode);
        map.put("account", account);
        return userMapper.getRolesByAccount(map);
    }

    @Override
    public AuthUser getUserInfoById(String userId) {
        AuthUser authUser = new AuthUser();
        authUser.setUserId(Long.valueOf(userId));
        // 未删除的用户
        authUser.setDelTag(0);
        // 未禁用的用户
        authUser.setUserStatus((byte) 0);
        List<AuthUser> authUsers = authUserMapper.getUserById(authUser);
        return authUsers.size() == 1 ? authUsers.get(0) : null;
    }

    @Override
    public List<Site> getUserSiteByAccount(String account) {
        return userMapper.getUserSiteByAccount(account);
    }

    @Override
    public List<AuthFunction> findAllFunctionsBySite(Long siteId) {
        return userMapper.findAllFunctionsBySite(siteId);
    }

    @Override
    public Set<String> findPermissionsBySite(Long siteId) {
        return userMapper.findPermissionsBySite(siteId);
    }

}
